package com.know.web;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;

import my.mvc.RequestContext;
import my.util.ResponseUtils;

import org.apache.commons.lang.StringUtils;

import com.know.entity.Lables;
import com.know.entity.Users;
import com.know.service.CommentService;
import com.know.service.ImageService;
import com.know.service.UserService;
import com.kzw.bean.Record;
import com.kzw.dao.support.Page;
import com.kzw.json.Jackson;

public class UserAction {

	
	private UserService userservice=new UserService();
	private CommentService commentService=new CommentService();
	private ImageService imageService=new ImageService();
	private SessionLink link=new SessionLink();
	
	/**
	 * 表格加载列表
	 * */
	public void talist(RequestContext rc) throws ServletException, IOException {
		int pageNo = rc.param("page", 1);
		int pageSize = rc.param("limit", 5);
		String orderBy = rc.param("orderBy", "");
		String orderDir = rc.param("orderDir", "");
		
		Page<Users> page = new Page<>(pageNo, pageSize);
		if(StringUtils.isNotBlank(orderBy)) {
			page.setOrderBy(orderBy);
			page.setOrder(orderDir);
		}
		
		Record record = userservice.findPage(page);
		// 输出JSON
		String json = Jackson.me().toJson(record);
		//System.out.println(json);
		ResponseUtils.renderJson(rc.response(), json);
	}
	/**
	 * 登陆用户，实现密码核对。
	 */
	public void login(RequestContext rc) throws ServletException, IOException {
		String user_name=rc.param("username");
		String user_password=rc.param("password");
		String yzm=rc.param("yzm");
		Record record=new Record(false);
		String msg="";
		//账号验证
		if(!isVer(rc,yzm))
		{
			msg="验证码错误";
			
		}
		else if(userservice.isUser(user_name,user_password))//验证码验证，账号密码验证
		{
			//写入seesion，跳转主页
			record=link.login(rc);
			
			msg="成功登陆";

		}
		else
		{
			msg="账号或密码错误";
			
		}
		record.setMsg(msg);
		String json =Jackson.me().toJson(record);
		System.out.println(json);
		ResponseUtils.renderJson(rc.response(), json);
	}
	/**
	 * 普通用户登录状态检查
	 */
	public void checkLogin(RequestContext rc) throws ServletException, IOException {
		Users user=null;
		Record record=new Record(false);
		if(rc.sessionAttr("user")!=null)
			user=(Users)rc.sessionAttr("user");
		if(user!=null){
			rc.reqAttr("user", user);
			record.setSuccess(true);
		}
		if(user!=null)
		{
			//有登陆信息,允许进入个人中心
			record.setSuccess(true);
			record.setData(user);
		}
		
		String json=Jackson.me().toJson(record);
		ResponseUtils.renderJson(rc.response(),json);
		
	}
	public void update(RequestContext rc) throws ServletException, IOException {
		Users user=rc.form(Users.class);
		//读取图片UUID字符串
		String uuid=rc.param("imgpid");
				//进行拆分
		String []uuids = null;
		if(uuid!=null)
			uuids=uuid.split(",");
		Record record=new Record(false);
		if(user!=null)
		{
			//更新数据库
			record.setSuccess(true);
			record=userservice.update(user);
			record.setMsg("成功修改账号信息");
			if(uuids!=null)
			//更新头像
				imageService.update(uuids, user.getUser_id());

		}
		
		String json=Jackson.me().toJson(record);
		ResponseUtils.renderJson(rc.response(),json);
		
	}
	/**
	 * 账号密码修改
	 * @param rc
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updatepass(RequestContext rc) throws ServletException, IOException {
		Users user=rc.form(Users.class);
		String repassword=rc.param("user_repassword");
		Record record=new Record(false);
		if(user.getUser_password().equals(repassword))
		{
			//更新数据库
			record.setSuccess(true);
			record=userservice.updatepass(user);
			record.setMsg("成功修改密码");
		}
		
		String json=Jackson.me().toJson(record);
		ResponseUtils.renderJson(rc.response(),json);
		
	}
	/**
	 * 进行普通账号注册
	 * @param rc
	 * @throws ServletException
	 * @throws IOException
	 */
	public void reg(RequestContext rc) throws ServletException, IOException {
		Users user=rc.form(Users.class);//获取用户信息用于注册
		String user_repassword=rc.param("user_repassword");
		String yzm=rc.param("yzm");//得到验证码
		String msg="成功注册";
		System.out.println();
		Record record=new Record(false);
		//进行合法性验证
		if(!isVer(rc,yzm))
		{
			msg="验证码错误";
		}
		else if(!userservice.isUser(user.getUser_name()))
		{
			
			msg="账号重复";
		}
		else if(!user_repassword.equals(user.getUser_password()))
		{
			msg="两次密码不一致";
		}
		else
		{
			//进行数据库写入
			record.setSuccess(true);
			user.setUser_reg_time(new Date());
			userservice.save(user);
			msg="成功注册";
			
		}
		
	
		System.out.println(msg+user.getUser_name()+","+user.getUser_password());
		record.setMsg(msg);
		String json =Jackson.me().toJson(record);
		System.out.println(json);
		ResponseUtils.renderJson(rc.response(), json);
	}
	/**
	 * 登陆，注册验证码验证
	 * @param rc
	 * @param yzm
	 * @return
	 */
	private boolean isVer(RequestContext rc,String yzm){
		String CheckCode=rc.sessionAttr("randCheckCode").toString();
		
		return userservice.isVer(CheckCode,yzm);
		
	}
	/**
	 * 普通账号回显信息(
	 */
	public void getone(RequestContext rc) throws ServletException, IOException {
		Users user=null;
		Record record=new Record(false);
		if(rc.sessionAttr("user")!=null)
			user=(Users)rc.sessionAttr("user");
		if(user!=null){
			rc.reqAttr("user", user);
			record.setSuccess(true);
		}
		String json=Jackson.me().toJson(record);
		rc.forward("/user_center.jsp");
		ResponseUtils.renderJson(rc.response(),json);
			
		
			
	}
	/**
	 * 管理员用户登录状态检查
	 */
	public void checkadLogin(RequestContext rc) throws ServletException, IOException {
		Users user=null;
		Record record=new Record(false);
		if(rc.sessionAttr("user")!=null)
			user=(Users)rc.sessionAttr("user");
		if(user!=null&&user.getUser_name().equals("admin")){
			
			rc.reqAttr("user", user);
			record.setSuccess(true);
			System.out.println("管理员账号："+user.getUser_name());

			//有登陆信息,允许进入个人中心
			record.setSuccess(true);
			record.setData(user);
		}
		
		
		String json=Jackson.me().toJson(record);
		ResponseUtils.renderJson(rc.response(),json);
		
	}
	/**
	 * 管理账号回显信息(
	 */
	public void getuser(RequestContext rc) throws ServletException, IOException {
		String id=rc.param("id");
		if(id!=null && !id.equals("")) {
			Users user = userservice.findById(Long.parseLong(id));
			System.out.println("管理员账号："+user.getUser_name());
			rc.reqAttr("user", user);
			rc.forward("/admin/index.jsp");
		}
			
		
			
	}
	/**
	 * 管理员修改
	 * @param rc
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateall(RequestContext rc) throws ServletException, IOException {
		Users user=rc.form(Users.class);
		Record record=new Record(false);
		
		if(user.getUser_id()==0) {
			// 增加
			userservice.save(user);
		} else {
			// 修改
			record=userservice.updateall(user);
		}
			//更新数据库
			record.setMsg("成功操作");
		
		
		String json=Jackson.me().toJson(record);
		ResponseUtils.renderJson(rc.response(),json);
		
	}
	
	/**
	 * 进行管理员账号登陆
	 * 
	 */
	public void adlogin(RequestContext rc) throws ServletException, IOException {
		
		rc.forward("/admin/login.jsp");
	}
	/**
	 * 管理员验证跳转
	 * @param rc
	 * @throws ServletException
	 * @throws IOException
	 */
	public void adcheck(RequestContext rc) throws ServletException, IOException {
		String user_name=rc.param("username");
		String user_password=rc.param("password");
		Record record=new Record(false);
		String msg="";
		//账号验证
		if(!user_name.equals("admin"))
		{
			msg="不是管理员";
		}
		else if(userservice.isUser(user_name,user_password))//验证码验证，账号密码验证
		{
			//写入seesion，跳转主页
			record=link.login(rc);
			
			msg="成功登陆";

		}
		else
		{
			msg="账号或密码错误";
			
		}
		record.setMsg(msg);
		String json =Jackson.me().toJson(record);
		System.out.println(json);
		ResponseUtils.renderJson(rc.response(), json);
	}
	/**
	 * 首页热评用户读取
	 */
	public void hotusers(RequestContext rc) throws ServletException, IOException {
		List<Users> list =userservice.hotusers(7);//读取7个用户
		String json =Jackson.me().toJson(list);
//		System.out.println(json);
		ResponseUtils.renderJson(rc.response(), json);
	}
}
